<?php

namespace App\Http\Controllers\AppHome\AuthManage;

use App\Http\Controllers\Controller;
use App\Models\AppHome\AuthManage\LoginModel;
use Illuminate\Http\Request;

/**
 * 登录控制类
 * Class LoginController
 * @package App\Http\Controllers
 */
class LoginController extends Controller
{
	/**
	 * 用户登录
	 * @param Request $request
	 * @return false|string
	 * @throws \Exception
	 */
	public function login(Request $request)
	{
		$userName = $request->input('userName', '');
		$userPwd = $request->input('userPwd', '');
		$relData = LoginModel::LoginUser($userName, $userPwd);
		return ApiReturn($relData);
	}

	/**
	 * 更新当前TOKEN
	 * @return false|string
	 * @throws \Exception
	 */
	public function updateToken()
	{
		$relData = LoginModel::updateToken(APP_USER_TOKEN);
		return ApiReturn($relData);
	}

    /**
     * 退出登录
     * @return false|string
     * @throws \Exception
     */
	public function loginOut()
	{
		$relData = LoginModel::LoginOut(APP_USER_TOKEN);
		return ApiReturn($relData);
	}

    /**
     * 获取用户信息
     * @return false|string
     * @throws \Exception
     */
	public function getUserInfo()
	{
		$userInfoObj = LoginModel::getUserInfo(APP_USER_TOKEN);
		if ($userInfoObj['code'] != RETURN_SUCCESS) {
			return ApiReturn($userInfoObj);
		}

		$userObj = $userInfoObj['data'];
		unset($userObj['authList'], $userObj['uid']);
		return ApiReturn(RETURN_FAIL, "获取成功", $userObj);
	}

    /**
     * 用户注册
     * @param Request $request
     * @return false|string
     * @throws \Exception
     */
	public function register(Request $request) {
        $userName = $request->input('userName', '');
        $userPwd = $request->input('userPwd', '');
        if (!$userName) {
            return ApiReturn(RETURN_CHECK_PARAMS_FAIL, '登录账户不能为空');
        }

        if (strlen($userName) < 6) {
            return ApiReturn(RETURN_CHECK_PARAMS_FAIL, '登录账户长度不能小于6');
        }

        if (!$userPwd) {
            return ApiReturn(RETURN_CHECK_PARAMS_FAIL, '登录密码不能为空');
        }

        if (strlen($userPwd) < 6) {
            return ApiReturn(RETURN_CHECK_PARAMS_FAIL, '登录密码长度不能小于6');
        }
        $relData = LoginModel::register($userName, $userPwd);
        return ApiReturn($relData);
    }

    /**
     * 修改用户密码
     * @param Request $request
     * @return false|string
     * @throws \Exception
     */
    public function updatePwd(Request $request) {
        $oldPwd = $request->input('oldPwd', '');
        $newPwd = $request->input('newPwd', '');

        if (strlen($oldPwd) < 6 || strlen($newPwd) < 6) {
            return ApiReturn(RETURN_CHECK_PARAMS_FAIL, '密码不能小于6位');
        }

        if ($oldPwd === $newPwd) {
            return ApiReturn(RETURN_CHECK_PARAMS_FAIL, '新密码不能与最近一次密码一致');
        }

        $relData = LoginModel::updatePwd(APP_USER_TOKEN, $oldPwd, $newPwd);
        return ApiReturn($relData);
    }

    /**
     * 管理员重置个人密码
     * @param Request $request
     * @return false|string
     * @throws \Exception
     */
    public function resetUserPwd(Request $request) {
        $newPwd = $request->input('newPwd', '');

        if ($newPwd && strlen($newPwd) < 6) {
            return ApiReturn(RETURN_CHECK_PARAMS_FAIL, '密码不能小于6位');
        }

        $relData = LoginModel::resetUserPwd(APP_USER_TOKEN, $newPwd);
        return ApiReturn($relData);
    }
}
